package cn.stylefeng.guns.log;

import cn.stylefeng.roses.core.util.HttpContext;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: LogAspect
 * @Author liuqiang
 * @Date 2020-02-14 13:14
 */
@Aspect
@Component
@Slf4j
public class LogAspect {
//    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    @Pointcut("execution(public * cn.stylefeng.guns.modular..*.controller..*.*(..))")
    private void point() {

    }
    
    /**
     * 在切点之前织入
     * @param joinPoint
     * @throws Throwable
     */
    @Before("point()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 开始打印请求日志
//        HttpServletRequest request = HttpContext.getRequest();
//        String tranceId = TraceIdGenerator.generateString();
//
//        threadLocal.set(tranceId);
//
//        log.info("RequestId={},RequestUrl={},RequestIp={},RequestArgs={}",tranceId,request.getRequestURL().toString(),request.getRemoteAddr(),JSONObject.toJSONString(joinPoint.getArgs()));
        // 打印请求相关参数
//        log.info("========================================== Start ==========================================");
//        // 打印请求 url
//        log.info("requestID={} URL: {}", request.getRequestURL().toString());
//        // 打印 Http method
//        log.info("HTTP Method    : {}", request.getMethod());
//        // 打印调用 controller 的全路径以及执行方法
//        log.info("Class Method   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
//        // 打印请求的 IP
//        log.info("IP             : {}", request.getRemoteAddr());
//        // 打印请求入参
//        log.info("Request Args   : {}", JSONObject.toJSONString(joinPoint.getArgs()));
    }

    /**
     * 在切点之后织入
     * @throws Throwable
     */
//    @After("point()")
//    public void doAfter() throws Throwable {
//        log.info("=========================================== End ===========================================");
//        // 每个请求之间空一行
//        log.info("");
//    }

    /**
     * 环绕
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("point()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        HttpServletRequest request = HttpContext.getRequest();
        String requestId = MDC.get("reqId");

        log.info("RequestId={},RequestUrl={},RequestIp={},RequestArgs={}",requestId,request.getRequestURL().toString(),request.getRemoteAddr(),JSONObject.toJSONString(proceedingJoinPoint.getArgs()));

        Object result = proceedingJoinPoint.proceed();

        // 打印出参
        log.info("RequestID={},ResponseArgs={},Time-Consuming : {} ms", requestId,JSONObject.toJSONString(result),System.currentTimeMillis() - startTime);
        return result;
    }


}
